package com.dao.impl;

import com.dao.BillDao;
import com.dao.ProviderDao;
import com.pojo.Bill;
import com.utils.MyJdbcTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

@Repository
public class BillDaoImpl implements BillDao {

    @Autowired
    private MyJdbcTemplate jdbcTemplate;
    @Autowired
    private ProviderDao providerDao;

    public int addBillDao(Bill bill) {
        String sql = "insert into smbms_bill(billCode,productName,productUnit,productCount,totalPrice,isPayment,providerId,creationDate) " +
                "values(?,?,?,?,?,?,?,?)";
        return jdbcTemplate.update(sql,bill.getBillCode(),bill.getProductName(),bill.getProductUnit(),bill.getProductCount(),bill.getTotalPrice(),bill.getIsPayment(),bill.getProviderId().getId(),new Date());
    }

    public int deleteBillDao(int id) {
        String sql = "delete from smbms_bill where id=?";
        return jdbcTemplate.update(sql,id);
    }

    public int updateBillDao(Bill bill) {
        String sql = "update smbms_bill set billCode=?,productName=?,productUnit=?,productCount=?,totalPrice=?,isPayment=?,providerId=? where id=?";
        return jdbcTemplate.update(sql,bill.getBillCode(),bill.getProductName(),bill.getProductUnit(),bill.getProductCount(),bill.getTotalPrice(),bill.getIsPayment(),bill.getProviderId().getId(),bill.getId());
    }

    public Bill findBillByid(int id) {
        String sql = "select * from smbms_bill where id=?";
        return jdbcTemplate.queryForObject(sql, new RowMapper<Bill>() {
            @Override
            public Bill mapRow(ResultSet rs, int rowNum) throws SQLException {
                Bill bill = new Bill();
                bill.setId(rs.getLong("id"));
                bill.setBillCode(rs.getString("billCode"));
                bill.setProductName(rs.getString("productName"));
                bill.setProductDesc(rs.getString("productDesc"));
                bill.setProductUnit(rs.getString("productUnit"));
                bill.setProductCount(rs.getDouble("productCount"));
                bill.setTotalPrice(rs.getDouble("totalPrice"));
                bill.setIsPayment(rs.getInt("isPayment"));
                bill.setCreatedBy(rs.getLong("createdBy"));
                bill.setCreationDate(rs.getDate("creationDate"));
                bill.setProviderId(providerDao.findProviderById((int)rs.getLong("providerId")));
                return bill;
            }
        }, id);
    }

    public List<Bill> findAll() {
        String sql = "select * from smbms_bill";
        return jdbcTemplate.query(sql, new RowMapper<Bill>() {
            @Override
            public Bill mapRow(ResultSet rs, int rowNum) throws SQLException {
                Bill bill = new Bill();
                bill.setId(rs.getLong("id"));
                bill.setBillCode(rs.getString("billCode"));
                bill.setProductName(rs.getString("productName"));
                bill.setProductDesc(rs.getString("productDesc"));
                bill.setProductUnit(rs.getString("productUnit"));
                bill.setProductCount(rs.getDouble("productCount"));
                bill.setTotalPrice(rs.getDouble("totalPrice"));
                bill.setIsPayment(rs.getInt("isPayment"));
                bill.setCreatedBy(rs.getLong("createdBy"));
                bill.setCreationDate(rs.getDate("creationDate"));
                bill.setProviderId(providerDao.findProviderById((int) rs.getLong("providerId")));
                return bill;
            }
        });
    }

    @Override
    public List<Bill> searchBill(Bill bill) {
        String sql = "select * from smbms_bill where 1=1 ";
        if (bill.getProductName()!=""){
            sql = sql + " and productName REGEXP '" + bill.getProductName()+"'";
        }
        if (bill.getProviderId().getId()!=null){
            sql = sql+" and providerId = "+bill.getProviderId().getId();
        }
        if (bill.getIsPayment()!=null){
            /*需要改成Intger*/
            sql = sql + " and isPayment = " + bill.getIsPayment();
        }
        System.out.println(sql);
        return jdbcTemplate.query(sql, new RowMapper<Bill>() {
            @Override
            public Bill mapRow(ResultSet rs, int rowNum) throws SQLException {
                Bill bill = new Bill();
                bill.setId(rs.getLong("id"));
                bill.setBillCode(rs.getString("billCode"));
                bill.setProductName(rs.getString("productName"));
                bill.setProductDesc(rs.getString("productDesc"));
                bill.setProductUnit(rs.getString("productUnit"));
                bill.setProductCount(rs.getDouble("productCount"));
                bill.setTotalPrice(rs.getDouble("totalPrice"));
                bill.setIsPayment(rs.getInt("isPayment"));
                bill.setCreatedBy(rs.getLong("createdBy"));
                bill.setCreationDate(rs.getDate("creationDate"));
                bill.setProviderId(providerDao.findProviderById((int) rs.getLong("providerId")));
                return bill;
            }
        });
    }
}
